<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use think\Model;
use Wechat\Lib\Tools;
use app\comm\service\WeixinpayService;
use Endroid\QrCode\QrCode;
use \think\Loader;

class Wxrefund extends Controller{
    /**
     * @Author  lingyun
     * @Desc    订单退款   20180612102228105595000001  -- wx
     * bool(true)
     * bool(false)
     */
    public function index()
    {
        $order_id = input('order_id');          //订单id

        $order = db('order')->where('id', $order_id)->find();

        if(empty($order)){      //找不到订单信息
            $this->error('订单不存在，不能发起退款','');
        }else if($order['is_tuikuan'] == 1){            //订单已退款
            $this->error('该订单已退款，请勿重复退款','');
        }

        //微信浏览器端
        $pay = 	&load_wechat('Pay');                    //实例化支付类
        $order_result = $pay->queryOrder($order['order_sn']);            //查询订单详情
        $out_trade_no = $order_result['out_trade_no'];
        $transaction_id = $order_result['transaction_id'];
        $out_refund_no = 'R'.build_order_no();
        $total_fee = $order_result['cash_fee'];
        $refund_fee = $order_result['cash_fee'];

        $refund_result = $pay->refund($out_trade_no, $transaction_id, $out_refund_no, $total_fee, $refund_fee, $op_user_id = null, $refund_account = '');

        if($refund_result !== false){
            if($order['is_tuikuan'] != 1){
                $store = db('store')->where('id',$order['store_id'])->find();
                db('order')->where('id',$order['id'])->update(['order_status'=>6,'remark'=>'订单已退款','tuikuan_time'=>time(),'is_tuikuan'=>1]);     //更新订单状态
                $order_action_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'action_user'=>session('cash_user')['id'],
                    'order_status'=>6,
                    'pay_status'=>1,
                    'status_desc'=>'订单已退款',
                    'storeid'=>$order['store_id'],
                    'addtime'=>time(),
                ];
                db('order_action')->insert($order_action_log);
                $refund_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'amount'=>$order['final_amount'],
                    'casher'=>session('cash_user')['id'],
                    'addtime'=>time()
                ];
                db('order_refund')->insert($refund_log);


                //门店金额记录
                $store_credit_log = [
                    'store_id'=>$order['store_id'],
                    'order_id'=>$order['id'],
                    'type'=>3,
                    'credit'=>$order['final_amount'],
                    'cur_credit'=>$store['total_amount']-$order['final_amount'],
                    'admin_id'=>session('cash_user')['id'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'order_type'=>1,
                    'order_sn'=>$order['order_sn'],
                    'pay_type'=>$order['pay_type']
                ];
                db('store_credit_log')->insert($store_credit_log);      //保存数据

                if ($order['pay_type'] == 1) {
                    $fild = 'al_money';
                } else if ($order['pay_type'] == 2) {
                    $fild = 'wx_money';
                } else if ($order['pay_type'] == 3) {          //余额
                    $fild = 'user_money';
                } else if ($order['pay_type'] == 4) {
                    $fild = 'xianjin_money';
                } else if ($order['pay_type'] == 5) {
                    $fild = 'bank_money';
                } else if ($order['pay_type'] == 6) {
                    $fild = 'meituan_money';
                } else if ($order['pay_type'] == 7) {
                    $fild = 'elema_money';
                }

                $update_arr = [
                    'total_amount'=>$store['total_amount']-$order['final_amount'],
                    $fild=>$store[$fild] - $order['final_amount']+$order['pay_type_two'],
                    'user_money'=>$store['user_money'] - $order['pay_type_two']
                ];
                db('store')->where('id',$store['id'])->update($update_arr);         //更新门店金额

                $pay_log = [
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'order_type'=>empty($order['order_type'])?1:2,
                    'uid'=>$order['uid'],
                    'pay_type'=>$order['pay_type'],
                    'pay_sign'=>4,
                    'amount'=>$order['final_amount'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'store_id'=>$order['store_id']
                ];
                db('pay_log')->insert($pay_log);


//                db('store')->where('id',$order['store_id'])->setInc('tuikuan_amount',$order['final_amount']);       //退款订单金额
                $this->success('退款成功','');
            }else{
                $this->success('退款成功','');
            }
        }else{
            $this->error('退款失败','');
        }
    }


}